/// 内容分类模型
class Category {
  final String id;
  final String name;
  final String? icon;
  final String? color;
  final int? sortOrder;
  final bool? isActive;
  final DateTime? createTime;
  final DateTime? updateTime;
  final String? createBy;
  final String? updateBy;
  final bool? isDeleted;
  final String? remark;

  Category({
    required this.id,
    required this.name,
    this.icon,
    this.color,
    this.sortOrder,
    this.isActive,
    this.createTime,
    this.updateTime,
    this.createBy,
    this.updateBy,
    this.isDeleted,
    this.remark,
  });

  factory Category.fromJson(Map<String, dynamic> json) {
    return Category(
      id: json['id']?.toString() ?? '',
      name: json['name'] ?? '',
      icon: json['icon'],
      color: json['color'],
      sortOrder: json['sortOrder'],
      isActive: json['isActive'],
      createTime: json['createTime'] != null 
          ? DateTime.tryParse(json['createTime']) 
          : null,
      updateTime: json['updateTime'] != null 
          ? DateTime.tryParse(json['updateTime']) 
          : null,
      createBy: json['createBy'],
      updateBy: json['updateBy'],
      isDeleted: json['isDeleted'],
      remark: json['remark'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'icon': icon,
      'color': color,
      'sortOrder': sortOrder,
      'isActive': isActive,
      'createTime': createTime?.toIso8601String(),
      'updateTime': updateTime?.toIso8601String(),
      'createBy': createBy,
      'updateBy': updateBy,
      'isDeleted': isDeleted,
      'remark': remark,
    };
  }

  /// 获取创建时间，如果没有则返回当前时间
  DateTime get createdAt => createTime ?? DateTime.now();
}

